package com.dreamlin.lindownload.entity;

import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.dreamlin.lindownload.thread.LinThreadInfoImp;

import io.realm.Realm;
import io.realm.RealmObject;
import io.realm.RealmResults;
import io.realm.annotations.PrimaryKey;

/**
 * <p> Title: LinThreadBean </p>
 * <p> Description: </p>
 *
 * @author: dreamlin
 * @date: 2020/5/5
 * @version: V1.0.0
 * Created by dreamlin on 2020/5/5.
 */
public class LinThreadBean extends RealmObject implements LinThreadInfoImp {

    @PrimaryKey
    private String id;
    private String url;
    private String savePath;
    private String fileName;
    private long start;
    private long end;
    private long length = 0L;
    private long finished = 0L;
    private int autoRetry;
    private long callInterval;

    public LinThreadBean(String url, String savePath, String fileName, long start, long end,
                         long length, int autoRetry, long callInterval) {
        this.url = url;
        this.savePath = savePath;
        this.fileName = fileName;
        this.start = start;
        this.end = end;
        this.length = length;
        this.autoRetry = autoRetry;
        this.callInterval = callInterval;
    }

    public LinThreadBean() {
    }

    @NonNull
    @Override
    public String getId() {
        return id;
    }

    @Override
    public void setId(String id) {
        this.id = id;
    }

    @NonNull
    @Override
    public String getUrl() {
        return url;
    }

    @Override
    public void setUrl(String url) {
        this.url = url;
    }

    @NonNull
    @Override
    public String getSavePath() {
        return this.savePath;
    }

    @Override
    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    @NonNull
    @Override
    public String getFileName() {
        return fileName;
    }

    @Override
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public long getStart() {
        return start;
    }

    @Override
    public void setStart(long start) {
        this.start = start;
    }

    @Override
    public long getEnd() {
        return end;
    }

    @Override
    public void setEnd(long end) {
        this.end = end;
    }

    @Override
    public long getLength() {
        return length;
    }

    @Override
    public void setLength(long length) {
        this.length = length;
    }

    @Override
    public long getFinished() {
        return finished;
    }

    @Override
    public void setFinished(long finished) {
        this.finished = finished;
    }

    @Override
    public int getAutoRetry() {
        return autoRetry;
    }

    @Override
    public void setAutoRetry(int autoRetry) {
        this.autoRetry = autoRetry;
    }

    @Override
    public long getCallInterval() {
        return callInterval;
    }

    @Override
    public void setCallInterval(long callInterval) {
        this.callInterval = callInterval;
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void delete(Realm realm) {
        //delete from db
        if (realm != null && !realm.isClosed()) {
            RealmResults<LinThreadBean> ids = realm.where(LinThreadBean.class)
                    .equalTo("id", this.id)
                    .findAll();
            if (ids != null && !ids.isEmpty()) {
                realm.beginTransaction();
                ids.deleteAllFromRealm();
                realm.commitTransaction();
            }
        }
    }

    @Override
    public void update(Realm realm) {
        if (realm != null && !realm.isClosed()) {
            realm.beginTransaction();
            realm.copyToRealmOrUpdate(this);
            realm.commitTransaction();
        }
    }
}
